package com.yuzhihao.writejvm.attributes.runtimeva;

import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.format.PrintFormat;
import com.yuzhihao.writejvm.read.Parse;
import com.yuzhihao.writejvm.read.ReadClassFileFatory;
import com.yuzhihao.writejvm.read.TypeFatory;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * The value of the num_element_value_pairs item gives the number of element-value pairs of the annotation represented by this annotation structure.
 *
 * @author yzh
 * @since 2021年12月11日14:02:51
 */
@Data
@Log4j2
public class ElementValuePairs implements Parse {

    private int element_name_index_u2 = 2;

    private int element_name_index;

    private ElementValue ev;

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        try {
            this.element_name_index = Parse.byteToInt(this.element_name_index_u2, dis);

            char item = (char) Parse.byteToInt(ElementValue.tag_u1, dis);
            this.ev = ReadClassFileFatory.ev_instance(item);
            ev.parse(dis, ci);

        } catch (IOException e) {
            log.error(e);
        }
    }

    public String print(String print) {
        StringBuilder builder = new StringBuilder();
        builder.append(print).append(PrintFormat.EMPTY).append(this.ev.print(print,0));
        return builder.toString();
    }

}
